package com.vtest.quartz.service.impl.vtest.report;

import com.vtest.quartz.domain.vtest.report.CPPostErrorEntity;
import com.vtest.quartz.domain.vtest.report.HoldBinDataEntity;
import com.vtest.quartz.domain.vtest.report.HoldReasonEntity;
import com.vtest.quartz.domain.vtest.report.HoldSummaryEntity;
import com.vtest.quartz.mapper.vtest.report.CommonReportMapper;
import com.vtest.quartz.service.vtest.report.CommonReportService;
import com.vtest.quartz.utils.others.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2024-12-24 16:27
 * @Description :通用报表service层
 */

@Service
public class CommonReportServiceImpl implements CommonReportService {

    @Autowired
    private CommonReportMapper commonMapper;

    /**
     * 获取CP过账报错报表信息
     */
    @Override
    public List<CPPostErrorEntity> getCPPostErrorInfo() {
        List<CPPostErrorEntity> cpPostErrorInfoList = commonMapper.getCPPostErrorInfo();
        //设置序列号
        for (int i = 0; i < cpPostErrorInfoList.size(); i++) {
            CPPostErrorEntity cpPostErrorEntity = cpPostErrorInfoList.get(i);
            cpPostErrorEntity.setId(i + 1);
        }

        return cpPostErrorInfoList;
    }

    /**
     * 获取FT Hold报表LotNum信息
     */
    @Override
    public List<Map<String, String>> getHoldLotInfo(String code, String lot) {
        if (!StringUtils.isNotEmpty(lot)) lot = "";
        return commonMapper.getHoldLotInfo(code, lot);
    }

    /**
     * 获取HoldReason 报表的数据
     */
    @Override
    public List<HoldReasonEntity> getHoldReasonReport(String lotNum, String ft) {

        //获取基础信息
        List<HoldReasonEntity> holdReasonInfoList = commonMapper.getHoldReasonInfoByLotNumAndFT(lotNum, ft);

        //LotNum的集合
        List<String> lotNumList = holdReasonInfoList.stream().map(entity -> entity.getLotNum()).collect(Collectors.toList());

        //获取卡控信息
        List<Map<String, String>> controlLimitInfoList = commonMapper.getControlLimitByLotNumAndFT(lotNum, ft.substring(0, 3));

        /*List<HoldReasonEntity> finalHoldReasonList = holdReasonInfoList.stream().filter(
                        HoldReasonEntity -> controlLimitInfoList.stream().allMatch(
                                controlLimitMap -> controlLimitMap.get("LotNum").equals(HoldReasonEntity.getLotNum())))
                .collect(Collectors.toList());*/

        for (HoldReasonEntity holdReasonEntity : holdReasonInfoList) {
            String reason = holdReasonEntity.getComment();
            if (reason.contains("%") && reason.contains("[") && reason.contains("]") && reason.contains("as")) {
                String strYield = reason.split("as")[1].split("%")[0].split("\\[")[1];
                double yield = Double.parseDouble(StringUtils.isNotEmpty(strYield) ? strYield : "0");
                holdReasonEntity.setYield(yield);
            }
            if (!reason.contains("[") && !reason.contains("]") && reason.contains("%")) {
                String strYield = reason.split("as")[1].split("%")[0];
                double yield = Double.parseDouble(StringUtils.isNotEmpty(strYield) ? strYield : "0");
                holdReasonEntity.setYield(yield);
            }
            for (Map<String, String> map : controlLimitInfoList) {
                String controlItem = map.get("ControlItem");
                String strLimit = map.get("ControlLimit") + "";

                if (reason.contains(controlItem)) {
                    double limit = Double.parseDouble(StringUtils.isNotEmpty(strLimit) ? strLimit : "0");
                    holdReasonEntity.setLimit(limit);
                    break;
                }

                if (reason.contains("Yield Failed as") && (controlItem.contains("[By Lot Yield]")
                        || controlItem.contains("[By Lot Final Yield]") || controlItem.contains("[By Wafer Yield]"))) {
                    double limit = Double.parseDouble(StringUtils.isNotEmpty(strLimit) ? strLimit : "0");
                    holdReasonEntity.setLimit(limit);
                    break;
                }

                if (reason.contains("Lot OS Failed as") && controlItem.contains("[By Lot OpenShot]")) {
                    double limit = Double.parseDouble(StringUtils.isNotEmpty(strLimit) ? strLimit : "0");
                    holdReasonEntity.setLimit(limit);
                    break;
                }

                if (reason.contains("Bin") && controlItem.contains("[By BinCode]")) {
                    String bin = reason.toUpperCase().split("BIN")[1].split("FAILED")[0].trim();
                    //[By BinCode]: bin4
                    String limitBin = controlItem.toUpperCase().split(":")[1].split("BIN")[1];
                    if (bin.equals(limitBin)) {
                        double limit = Double.parseDouble(StringUtils.isNotEmpty(strLimit) ? strLimit : "0");
                        holdReasonEntity.setLimit(limit);
                        break;
                    }
                }
            }
        }

        List<HoldReasonEntity> finalHoldReasonList = holdReasonInfoList.stream().map(
                (HoldReasonEntity holdReasonEntity) -> {
                    String fl = holdReasonEntity.getLimit() > holdReasonEntity.getYield() ? "Low" : "High";
                    holdReasonEntity.setHighLow(fl);
                    return holdReasonEntity;
                }
        ).collect(Collectors.toList());


        return finalHoldReasonList;
    }

    /**
     * 获取HoldSummary 报表的数据
     */
    @Override
    public List<HoldSummaryEntity> getFTHoldSummaryReport(String LotNum, String FT) {

        //获取基础信息
        List<HoldSummaryEntity> SummaryInfoList = commonMapper.getFTHoldSummaryInfo(LotNum, FT);

        return SummaryInfoList;
    }

    /**
     * 获取BinData 报表的数据
     */
    @Override
    public List<HoldBinDataEntity> getFTHoldBinDataReport(String LotNum, String FT) {
        //获取基础信息
        List<HoldBinDataEntity> BinDataInfoList = commonMapper.getFTHoldBinDataInfo(LotNum, FT);

        //求总数
        int total = BinDataInfoList.stream().mapToInt(HoldBinDataEntity::getTotal).sum();

        //设置PF
        List<HoldBinDataEntity> list = BinDataInfoList.stream().map(holdBinDataEntity -> {
                    holdBinDataEntity.setFail("F");
                    String passBin = holdBinDataEntity.getPassBin();
                    String[] passNums = passBin.split(",");
                    for (String passNum : passNums) {
                        if (passNum == holdBinDataEntity.getSoftBin()) holdBinDataEntity.setFail("P");
                    }
                    return holdBinDataEntity;
                }
        ).collect(Collectors.toList());

        //设置bin占比
        List<HoldBinDataEntity> finalBinDataInfoList = list.stream().map(holdBinDataEntity -> {
            holdBinDataEntity.setFail(holdBinDataEntity.getTotal() / total + "");
            return holdBinDataEntity;
        }).collect(Collectors.toList());

        return finalBinDataInfoList;
    }

    /**
     * FT Hold 报表 更新最后发送时间
     */
    @Override
    public int updateFTHoldFinalSendTime(String code, String now) {
        return commonMapper.updateFTHoldFinalSendTime(code, now);
    }
}
